package commonClass.date;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class DateTime {
	private Calendar calendar=null;
	public DateTime(){
		calendar=new GregorianCalendar();
	}
	
	public String getDate(){
		StringBuffer buf=new StringBuffer();
		buf.append(calendar.get(Calendar.YEAR)).append("-");
		buf.append(this.addZero(calendar.get(Calendar.MONTH)+1, 2)).append("-");//月份要+1
		buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2)).append(" ");
		buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2)).append(":");
		buf.append(this.addZero(calendar.get(Calendar.MINUTE), 2)).append(":");
		buf.append(this.addZero(calendar.get(Calendar.SECOND), 2)).append(".");
		buf.append(this.addZero(calendar.get(Calendar.MILLISECOND), 3));
		return buf.toString();
	}
	
	public String getDateComplete(){
		StringBuffer buf=new StringBuffer();
		buf.append(calendar.get(Calendar.YEAR)).append("年");
		buf.append(this.addZero(calendar.get(Calendar.MONTH)+1, 2)).append("月");//月份要+1
		buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2)).append("日 ");
		buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2)).append("时");
		buf.append(this.addZero(calendar.get(Calendar.MINUTE), 2)).append("分");
		buf.append(this.addZero(calendar.get(Calendar.SECOND), 2)).append("秒");
		buf.append(this.addZero(calendar.get(Calendar.MILLISECOND), 3)).append("毫秒");
		return buf.toString();
	}
	
	/**
	 * 得到时间戳   yyyyMMddHHmmssSSS
	 * @return
	 */
	public String getTimeStamp(){
		StringBuffer buf=new StringBuffer();
		buf.append(calendar.get(Calendar.YEAR));
		buf.append(this.addZero(calendar.get(Calendar.MONTH)+1, 2));//月份要+1
		buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2));
		buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2));
		buf.append(this.addZero(calendar.get(Calendar.MINUTE), 2));
		buf.append(this.addZero(calendar.get(Calendar.SECOND), 2));
		buf.append(this.addZero(calendar.get(Calendar.MILLISECOND), 3));
		return buf.toString();
	}
	/**
	 * 考虑到日期中有前导0
	 * @param num
	 * @param len
	 * @return
	 */
	private String addZero(int num,int len){
		StringBuffer s=new StringBuffer();
		s.append(num);
		while(s.length()<len){
			s.insert(0, "0");
		}
		return s.toString();
	}

	
	public static void main(String args[]){
		DateTime dateTime=new DateTime();
		System.out.println(dateTime.getDate());
		System.out.println(dateTime.getDateComplete());
		System.out.println(dateTime.getTimeStamp());
	}
}
